using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DogFishBallet : MonoBehaviour
{
    // Start is called before the first frame update
    public int face;
    public float demage;
    public float Runspeed;
    private Rigidbody2D myRigidbody;
    EnemyAttack enemyattack;
    public float damageratio;
    public int RepelTimes;
    public float RepelDistance, StiffTime;
    private PolygonCollider2D collider2D;
    private GameObject attackobject;
    private GameObject[] creations;
    public float dietime;
    Transform playertransform, transform;
    private void getplayer()
    {
        playertransform = attackobject.GetComponent<Transform>();
        creations = GameObject.FindGameObjectsWithTag("Creation");
        foreach (GameObject creation in creations)
        {
            if (Mathf.Abs(creation.GetComponent<Transform>().position.x - transform.position.x) < Mathf.Abs(playertransform.position.x - transform.position.x))
            {
                playertransform = creation.GetComponent<Transform>();
            }
        }
    }
    void Start()
    {
        myRigidbody = GetComponent<Rigidbody2D>();
        collider2D = GetComponent<PolygonCollider2D>();
        attackobject = GameObject.FindWithTag("Player");
        transform = GetComponent<Transform>();
        getplayer();
        if (playertransform.position.x < transform.position.x) face = -1;
        else face = 1;
        demage *= EnemyData.damageratio;
    }
    private void OnTriggerStay2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {

            other.GetComponent<PlayerBasic>().TakeDamage(demage, -face, RepelTimes, RepelDistance, StiffTime);
            Destroy(gameObject);
        }
        else if (other.gameObject.CompareTag("Shield"))
        {

            other.GetComponent<shield>().TakeDamage(demage);
            Destroy(gameObject);
        }
        else if (other.gameObject.CompareTag("Creation"))
        {

            other.GetComponent<Creation>().TakeDamage(demage);
            Destroy(gameObject);
        }
    }
    // Update is called once per frame
    void Update()
    {
        dietime -= Time.deltaTime;
        if (dietime <= 0) Destroy(gameObject);
        Vector2 vel = new Vector2(face * Runspeed, myRigidbody.velocity.y);
        myRigidbody.velocity = vel;
    }
}
